/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package demogroup5client.model;

import entity.Customer;
import java.util.ArrayList;
import entity.Product;
import javax.swing.JLabel;
import javax.swing.table.AbstractTableModel;
/**
 *
 * @author DungTV
 */
public class ProductModel extends AbstractTableModel{
    private ArrayList<Product> listProduct ;
    private Customer customer;
    private int currentPage;
    private final int rowsPerPage =10;
    private int totalPage;

    private static final ColumnData columnsCustomer[]  ={
        new ColumnData("ProductID",10,JLabel.LEFT),
        new ColumnData("ProductName",100,JLabel.LEFT),
        new ColumnData("CatID",100,JLabel.LEFT),
        new ColumnData("Description",100,JLabel.LEFT),
        new ColumnData("DateExpired",100,JLabel.LEFT),
        new ColumnData("Quantity",100,JLabel.LEFT),
        new ColumnData("Price",100,JLabel.LEFT),        
        new ColumnData("Production Date",100,JLabel.LEFT),

    };
    private static final ColumnData columnsAdmin[]  ={
        new ColumnData("ProductID",10,JLabel.LEFT),
        new ColumnData("ProductName",100,JLabel.LEFT),
        new ColumnData("CatID",100,JLabel.LEFT),
        new ColumnData("Description",100,JLabel.LEFT),
        new ColumnData("DateExpired",100,JLabel.LEFT),
        new ColumnData("Quantity",100,JLabel.LEFT),
        new ColumnData("Price",100,JLabel.LEFT),
        new ColumnData("Note",100,JLabel.LEFT),
        new ColumnData("Production Date",100,JLabel.LEFT),
        new ColumnData("Enable",100,JLabel.LEFT),
    };
   public static ColumnData columns[] = columnsAdmin;
   

    public ProductModel() {        
        this.listProduct = new ArrayList<Product>();
        customer = new Customer();
        customer.setAdmin(false);
        this.setColumnData();
        this.currentPage = 1;
        this.totalPage =1;
    }

    

    public ProductModel(ArrayList<Product> listProduct,Customer customer) {
        this.listProduct = listProduct;
        this.customer = customer;
        this.setColumnData();
        if (this.listProduct ==null) {
            this.listProduct = new ArrayList<Product>();
        }
        this.currentPage = 1;
        this.totalPage =this.listProduct.size()/this.rowsPerPage+1;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
        this.fireModel();
    }




    public ArrayList<Product> getListProduct() {
        return listProduct;
    }

    
    public void setListProduct(ArrayList<Product> listProduct) {
        this.listProduct = listProduct;
        if (this.listProduct == null){
            this.listProduct = new ArrayList<Product>();
        }
        this.totalPage =this.listProduct.size()/this.rowsPerPage+1;
        fireModel();
    }

    

    public void setColumnData(){
        if (this.customer.isAdmin()){
            ProductModel.columns = ProductModel.columnsAdmin;
        } else ProductModel.columns = ProductModel.columnsCustomer;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (customer.isAdmin()) return true;
        else return false;

    }

    @Override
     public int getRowCount() {
        int rowCount = this.listProduct.size();
        if (rowCount<this.rowsPerPage) return rowCount;
        else if (this.currentPage ==this.totalPage) return rowCount%this.rowsPerPage;
        else return this.rowsPerPage;         
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return columns[column].getTitle();
    }


    @Override
     public Object getValueAt(int rowIndex, int columnIndex) {
        Product product=listProduct.get(rowIndex+(this.currentPage-1)*this.rowsPerPage);
        switch(columnIndex) {            
            case 0:
                return product.getProductID();
            case 1:
                return product.getProductName();
            case 2:
                return product.getCatID();
            case 3:
                return product.getDescription();
            case 4:
                return product.getDateExpired();
            case 5:
                return product.getQuantity();
            case 6:
                return product.getPrice();
            case 7:
                return product.getNote();
            case 8:
                return product.getProductionDate();
             case 9:
                return product.isEnable();            
        }
        return null;
    }

     @Override
    public void setValueAt(Object value, int nRow, int nCol) {
        Product product=listProduct.get(nRow+(this.currentPage-1)*this.rowsPerPage);
        switch(nCol) {
            case 0:
                return;
            case 1:
                 product.setProductID((String) value);
                 fireModel();
                 break;
            case 2:
                product.setProductName(((String) value));
                fireModel();
                break;
            case 3:
                 product.setCatID((String) value);
                 fireModel();
                 break;
            case 4:
                 product.setDescription((String) value);
                 fireModel();
                 break;
            case 5:
                product.setDateExpired((String) value);
                fireModel();
                break;
            case 6:
                product.setQuantity((Long) value);
                fireModel();
                break;
            case 7:
                 product.setPrice((Double) value);
                 fireModel();
                 break;
            case 8:
                 product.setNote((String) value);
                 fireModel();
                 break;
            case 9:
                product.setProductionDate((String) value);
                fireModel();
                break;
             case 10:
                 product.setEnable((Boolean) value);
                 fireModel();
                 break;
        }
        
    }

    public Product getProductAt(int rowIndex){
            return this.listProduct.get(rowIndex+(this.currentPage-1)*this.rowsPerPage);
        }
    public void RemoveRow(int rowIndex){
        this.listProduct.remove(rowIndex);
        this.totalPage =this.listProduct.size()/this.rowsPerPage+1;
        if (this.currentPage > this.totalPage) this.currentPage = this.totalPage;
        this.fireModel();
    }

    public void fireModel() {
        fireTableDataChanged();
    }

}
